package com.erp.utils;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @ClassName HttpSessionUtils
 * @Description 会话工具类
 * @Author songjizhen
 * @Date 2020-07-16 23:25
 * @Version 1.0
 */
public class HttpSessionUtils implements Serializable {
    private  static Map<String,HttpSession> map=new HashMap<String,HttpSession>();

    /**
     * 获取会话集合
     * @return
     */
    public static Map<String, HttpSession> getMap() {
        return map;
    }

    /**
     * 存入会话
     * @param session
     */
    public static void set(HttpSession session){
         map.put(session.getId(), session);
    }

    /**
     * 根据会话id获取会话
     * @param sessionid
     * @return
     */
    public static HttpSession get(String sessionid){
        return map.get(sessionid);
    }
    public  static void remove(String sessionId){
        for(Iterator<Map.Entry<String,HttpSession>> it=map.entrySet().iterator();it.hasNext();){
            Map.Entry<String,HttpSession> session=it.next();
            if(session.getKey().equals(sessionId)){
                it.remove();
                break;
            }
        }
    }
}
